package com.nxy.cpfsfe.busi.bbatproc;




import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.nxy.cpfsfe.enumtype.*;
import com.nxy.cpfsfe.mybatis.mapper.ToSysNtcMapper;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.DateUtil;
import com.nxy.cpfsfe.util.ResponseUtil;
import com.nxy.cpfsfe.util.RpcCommUtil;
import com.nxy.cpfsfe.util.XmlUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;
import java.util.Map;

/**
 * 
 * Description: 自由格式报文请求NPS.303来账处理
 * @author 
 * @date 2019年9月19日
 */
public class BBatFreeFmtRmSrvBean implements CpfsService{
	
	private final static Logger LOGGER = LoggerFactory.getLogger(BBatFreeFmtRmSrvBean.class);
	SqlSession sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");	
	
	@Override
	public void doService(){	
	
		bBatFreeFmtRmSrv();
	
	}
	
	public void bBatFreeFmtRmSrv() {

		Map  bizmap= null;
		try {
			String tpId = DtaInfo.getInstance().getTpId();	
		    CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口		
			String bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):"";		
			String sndAppCd = (String) EPOper.get(tpId, "ReqBean[0].head[0].sndAppCd");	
			bizmap = xml2BizMap(bizMsg,sndAppCd);	

			ToSysNtcMapper toSysNtc = sqlSession.getMapper(ToSysNtcMapper.class);
			toSysNtc.insert(bizmap);
			//由网关给NCS回复990
			//returnGWNPS990(bizmap);
			} catch (DataAccessException e) {
				LOGGER.error("303自由格式信息入库异常,msgId=["+bizmap.get("msgId")+"]",e);
				throw new BaseException(RespCd.NCPS5002, e, "自由格式来账:msgId=["+bizmap.get("msgId")+"]");
			}catch (Throwable e) {
				throw new BaseException(RespCd.NCPO0101, e, "自由格式报文交易错");
			}
	}
	
	
	public String returnGWNPS990(Map<String, String> bizmap){
		String tpId = DtaInfo.getInstance().getTpId();	
		String rspCd = "";
		try {
			String bizMsg990 = ResponseUtil.build990Xml(bizmap);		
			//转发给网关
		    LOGGER.debug("rpc调用网关发送NPS990开始......");
		    EPOper.put(tpId, "ReqBean[0].head[0].sndAppCd", SysEcodeEnum.NCS.getCode());
		    EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd", SysEcodeEnum.NCS.getCode());
	    	EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", MsgCdOrTpEnum.MSGCD_990.getCode());
	    	//EPOper.put(tpId, "ReqBean[0].head[0].initrSeqno",bizmap.get("seqNb"));
		    EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg", bizMsg990);   
		    rspCd = RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());	
		} catch (Throwable e) {
			rspCd = RespCd.NCPO6047;
			LOGGER.error("自由格式返回网关990异常,msgId=["+bizmap.get("msgId")+"]",e);
		}
		return rspCd;
	}
	
	/**
	 * 解析报文并做特殊处理
	 * @param bizMsg
	 * @param sndAppCd
	 * @return
	 */
	public Map xml2BizMap (String bizMsg,String sndAppCd){
		Map<String, Object> map=null;
		try{
			Map xml2BizMap = XmlUtil.xml2Map(bizMsg);	
			map = xml2BizMap;	
			map.put("sndOrgNo", map.get("/transaction/header/msg/sndMbrCd"));
			map.put("recpOrgNo", map.get("/transaction/header/msg/rcvMbrCd"));
			map.put("ntcCont", map.get("/transaction/body/FreeFrmt/FreeFrmtInf/MsgCntt"));
			map.put("msgId", map.get("/transaction/body/FreeFrmt/GrpHdr/MsgId"));
			map.put("seqNb", map.get("/transaction/header/msg/seqNb"));
			map.put("ntcTyp",MsgCdOrTpEnum.MSGCD_303.getCode());
			map.put("bizSta", StatEnum.BIZSTA_SUC.getCode());
			map.put("ntcTm", DateUtil.getTimestamp(new Date()));
			map.put("Remark", "自由格式已收到");
			LOGGER.info("自由格式来账插表参数map为：[{}]" , map.toString());
			}catch (Throwable e){
				throw new BaseException(RespCd.NCPO0101,e,MsgCdOrTpEnum.MSGCD_303.getCode()+"报文解包异常");
			}
		return map;
		
	}
		
}
